package lc;

import java.util.Arrays;

import org.junit.Test;

public class Ex59 {
    class Solution {
        public int[][] generateMatrix(int n) {
            int l = 0, r = n - 1, u = 0, b = n - 1;
            int cur = 1;
            int[][] nums = new int[n][n];
            while (l <= r && u <= b) {
                if (l == r && u == b) {
                    nums[l][u] = cur++;
                }
                for (int i = l; i < r; i++) {
                    nums[u][i] = cur++;
                }
                for (int i = u; i < b; i++) {
                    nums[i][r] = cur++;
                }
                for (int i = r; i > l; i--) {
                    nums[b][i] = cur++;
                }
                for (int i = b; i > u; i--) {
                    nums[i][l] = cur++;
                }
                l++;
                r--;
                u++;
                b--;
            }
            return nums;
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        System.out.println(Arrays.deepToString(s.generateMatrix(3)));
    }
}
